/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.game.stepbystep;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.LayerManager;

/**
 *
 * @author zinin
 */
public class GameLayerManager extends LayerManager{
    public final GameDesign gameDesign;
    public final MIDPCanvas canvas;
    private int worldWidth = 0;     // world geometry , in general it is bigger that display
    private int worldHeight = 0;
    private int dxDisplay = 0;      // visible display geometry
    private int dyDisplay = 0;
    private int xOrigDisplay = 0;   // display origin position
    private int yOrigDisplay = 0;
    private int xposFatman;         // sprite Fatman position
    private int yposFatman;
    private int xEyesightFatman;    // the minimal distance sprite Fatman can come close to display boundary
    private int yEyesightFatman;
    
    // states related data
    public final int STATE_IDLE = 0;           // for StateIdle class instance
    public final int STATE_CHARSELECTED = 1;   // for StateCharacterSelected
    public final int STATE_CHARDIRECTED = 2;   // for StateCharacterDirected
    
    private State stateArr[] = new State[STATE_CHARDIRECTED + 1];
    private State state = null;
    
    GameLayerManager(MIDPCanvas aCanvas) {
        canvas = aCanvas;
        gameDesign = new GameDesign();
        try {
            gameDesign.updateLayerManagerForWorld(this);
            
            // set Fatman to center in the screen
            xposFatman = canvas.DISP_WIDTH / 2;
            yposFatman = canvas.DISP_HEIGHT / 2;
            gameDesign.getFatman().setPosition(xposFatman, yposFatman);
            
            worldHeight = gameDesign.getOrchard().getHeight();
            worldWidth = gameDesign.getOrchard().getWidth();
            
            xOrigDisplay = 0;
            yOrigDisplay = 0;
            
            dxDisplay = worldWidth - canvas.DISP_WIDTH;
            dyDisplay = worldHeight - canvas.DISP_HEIGHT;

            xEyesightFatman = canvas.DISP_WIDTH / 6;
            yEyesightFatman = canvas.DISP_HEIGHT / 6;
            
            initStates();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    private void initStates(){
        stateArr[STATE_IDLE] = new StateIdle(this);
        stateArr[STATE_CHARSELECTED] = new StateCharacterSeleted(this);
        stateArr[STATE_CHARDIRECTED] = new StateCharacterDirected(this);
        state = stateArr[STATE_IDLE];
    }
    
    public void setState(int stateIdx){
        state = stateArr[stateIdx];
    }
    
    public void paint(Graphics g) throws Exception {
        state.onCanvasDrawing(); // update data depending on current state
        
        g.setColor(MIDPCanvas.BCGRND_COLOR);
        // paint the background white to cover old game objects
        // that have changed position since last paint.
        // here coordinates are given 
        // with respect to the graphics (canvas) origin:
        g.fillRect(0, 0, canvas.DISP_WIDTH, canvas.DISP_HEIGHT);
        // here coordinates are given 
        // with respect to the LayerManager origin:
        setViewWindow(xOrigDisplay, yOrigDisplay, canvas.DISP_WIDTH, canvas.DISP_HEIGHT);
        // call the paint funstion of the superclass LayerManager
        // to paint all of the Layers
        paint(g, canvas.CORNER_X, canvas.CORNER_Y);
    }

    void setPlayerMovingLeft(boolean b) {
        try {
            int step = (b) ? -1 : 1;
            int pos = gameDesign.getFatman().getX() + step;
            if((pos > xOrigDisplay + canvas.DISP_WIDTH - xEyesightFatman) || 
                    (pos < xOrigDisplay + xEyesightFatman)){
                xOrigDisplay += step;
            }else{
                xposFatman = pos;
                gameDesign.getFatman().setPosition(xposFatman, yposFatman);
                gameDesign.getFatman().nextFrame();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    void setPlayerMovingUp(boolean b) {
        try {
            int step = (b) ? -1 : 1;
            int pos = gameDesign.getFatman().getY() + step;
            if((pos > yOrigDisplay + canvas.DISP_HEIGHT - yEyesightFatman) || 
                    (pos < yOrigDisplay + yEyesightFatman)){
                yOrigDisplay += step;
            }else{
                yposFatman = pos;
                gameDesign.getFatman().setPosition(xposFatman, yposFatman);
                gameDesign.getFatman().nextFrame();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void setFatmanPos(int screenPosX, int screenPosY) throws IOException{
        xposFatman = screenPosX;
        yposFatman = screenPosY;
        gameDesign.getFatman().setPosition(xposFatman + xOrigDisplay, yposFatman + yOrigDisplay);
    }
    
    public void dragMapPosition(int xdelta, int ydelta, boolean isTheLast){
        if(state.isAllowDragging()){
            xOrigDisplay -= xdelta;
            yOrigDisplay -= ydelta;
            if(isTheLast){
                setState(STATE_IDLE);
            }
        }
    }

    void onTapScreen(int x, int y) {
        state.onTapScreen(x, y);
    }
    
    public int displayOriginX(){
        return xOrigDisplay;
    }
    
    public int displayOriginY(){
        return yOrigDisplay;
    }
}
